package com.loong.seller.handler;

import com.loong.common.constant.ShopAudit;
import com.loong.common.constant.ShopStatus;
import com.loong.common.domain.BaseQuery;
import com.loong.common.domain.Pager;
import com.loong.common.utils.TransformUtils;
import com.loong.seller.business.ShopBusiness;
import com.loong.seller.common.backend.param.ShopParam;
import com.loong.seller.common.backend.param.ShopQueryParam;
import com.loong.seller.common.backend.result.ShopResult;
import com.loong.seller.common.forehead.param.ShopQueryForeheadParam;
import com.loong.seller.common.forehead.result.ShopDetailForeheadResult;
import com.loong.seller.common.forehead.result.ShopForeheadResult;
import com.loong.seller.model.DeliveryWays;
import com.loong.seller.model.Shop;
import com.loong.seller.result.ShopDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 店铺服务
 * @Author: hzp
 * @Date: 2020-10-26 11:44
 */
@Component
@Slf4j
public class ShopHandler {
    @Resource
    private ShopBusiness shopBusiness;

    @Resource
    private ShopDeliveryHandler shopDeliveryHandler;

    @Resource
    private ShopContactHandler shopContactHandler;

    @Resource
    private DeliveryWaysHandler deliveryWaysHandler;


    /**
     * 保存店铺信息
     * @param param 店铺信息
     */
    public void save(ShopParam param){
        Shop shop = TransformUtils.transform(param, Shop.class);
        if(CollectionUtils.isEmpty(param.getDeliveryWaysIds())){
            shopBusiness.save(shop);
            return;
        }
        List<DeliveryWays> deliveryWaysList = deliveryWaysHandler.findDeliveryWaysByIds(param.getDeliveryWaysIds());
        param.setDeliveryWaysList(deliveryWaysList);
        saveShopDeliveryAndShop(param);
    }
    private void saveShopDeliveryAndShop(ShopParam param){
        shopBusiness.saveShop(param);
        shopDeliveryHandler.save(param);
    }

    private void saveShopDeliveryAndUpdateShop(ShopParam param){
        shopDeliveryHandler.save(param);
        shopBusiness.updateByPrimaryKeySelective(param);

    }

    /**
     * 更新店铺信息
     * @param param 店铺信息
     */
    public void update(ShopParam param){
        if(CollectionUtils.isEmpty(param.getDeliveryWaysIds())){
            //TODO 这里更新成空 和 不更新无法判断 不允许改成空
            shopBusiness.updateByPrimaryKeySelective(param);
            return;
        }
        List<DeliveryWays> deliveryWaysList = deliveryWaysHandler.findDeliveryWaysByIds(param.getDeliveryWaysIds());
        param.setDeliveryWaysList(deliveryWaysList);
        //清除店铺配送方式
        shopDeliveryHandler.deleteByShopId(param.getId());
        saveShopDeliveryAndUpdateShop(param);
    }

    /**
     *根据id删除店铺信息
     * @param id 用户的id
     */
    public void deleteById(Long id){
        shopBusiness.deleteById(id);
        //删除 配送方式
        shopDeliveryHandler.deleteByShopId(id);
        //删除联系方式
        shopContactHandler.deleteByShopId(id);

    }
    /**
     * 分页查询 后台使用
     * @param baseQuery 后台分页对象
     * @param param 后台查询参数
     * @return 返回后台需要结果集
     */
    public Pager<ShopResult> pageQuery(BaseQuery baseQuery, ShopQueryParam param){
        Pager<Shop> manageApisPager = shopBusiness.pageQuery(baseQuery, () -> shopBusiness.query(param));
        return TransformUtils.transformPage(manageApisPager, ShopResult.class);
    }
    /**
     * 分页查询 c端使用
     * @param baseQuery 分页查询对象
     * @param param c端查询条件参数
     * @return 返回c端需要结果集
     */
    public Pager<ShopForeheadResult> pageQuery(BaseQuery baseQuery, ShopQueryForeheadParam param){
        Pager<Shop> manageApisPager = shopBusiness.pageQuery(baseQuery, () -> shopBusiness.query(TransformUtils.transform(param,ShopQueryParam.class)));
        return TransformUtils.transformPage(manageApisPager, ShopForeheadResult.class);
    }

    /**
     * 根据店铺id查找店铺详情包括联系方式
     * @param shopId
     * @return
     */
    public ShopDetail findDetailByShopId(Long shopId) {
       return shopBusiness.findShopDetailByShopId(shopId);
    }
    /**
     * 根据店铺id查找店铺详情包括联系方式 c端使用
     * @param shopId
     * @return
     */
    public ShopDetailForeheadResult findDetailByShopIdToForehead(Long shopId) {
        return TransformUtils.transform(shopBusiness.findShopDetailByShopId(shopId),ShopDetailForeheadResult.class);
    }

    /**
     * 根据店铺id查找店铺详情不包括联系方式 c端使用
     * @param id
     * @return
     */
    public ShopForeheadResult findShopByIdForehead(Long id){
        return TransformUtils.transform(shopBusiness.findById(id),ShopForeheadResult.class);
    }

    /**
     * 根据店铺id查找店铺详情不包括联系方式
     * @param id
     * @return
     */
    public ShopResult findShopById(Long id){
        return TransformUtils.transform(shopBusiness.findById(id),ShopResult.class);
    }

    /**
     * 审核店铺
     * @param shopId 店铺id
     * @param auditOperation 审核操作
     */
    public void  auditShop(Long shopId,Integer auditOperation){
        Shop shop = new Shop();
        shop.setId(shopId);
        if (auditOperation.equals(ShopAudit.APPROVE)){
            //审核通过，则店铺状态修改为已审核
            shop.setStatus(ShopStatus.AUDITED);
        }else if (auditOperation.equals(ShopAudit.REJECT)){
            //审核拒绝，则店铺状态修改为已审核未通过。
            shop.setStatus(ShopStatus.UNAPPROVED);
        }
        shopBusiness.updateByPrimaryKeySelective(shop);
    }

    public List<ShopForeheadResult> findUserShopCollectionByUserId(Long userId) {
        return TransformUtils.transformList(shopBusiness.findUserShopCollectionByUserId(userId),ShopForeheadResult.class);
    }
}
